import { Component } from '@angular/core';

import { AlertController, NavController, NavParams, ActionSheetController } from 'ionic-angular';

import { Storage } from '@ionic/storage';

import {MarketingLineupDetailService} from "./detail.service";
import {MarketingLineupSavePage} from "../save/save.component";

@Component({
  selector: 'page-marketing-lineup-detail',
  templateUrl: './detail.html',
  providers: [MarketingLineupDetailService]
})
export class MarketingLineupDetailPage {
	moa_page_name: any = "page-marketing-lineup-detail";
  moa_page_explain: any = "排队任务详情";

  detail: null;

  constructor(
    public alertCtrl: AlertController,
    public navCtrl: NavController,
    private navParams: NavParams,
    public actionSheetCtrl: ActionSheetController,
    public storage: Storage,
    public detailService: MarketingLineupDetailService
  ) {
      this.detail = this.navParams.get("detail");
  }

  delete() {
    let confirm = this.alertCtrl.create({
      title: '删除操作',
      message: "您确定要删除当前排队任务吗？",
      enableBackdropDismiss: false,
      buttons: [{
        text: '取消',
        role: 'cancel',
        handler: () => {

        }
      }, {
        text: '删除',
        handler: () => {
          this.detailService.deleteTask(this.detail["L_TASK_ID"]).subscribe(result => {
            console.log(result);
            if (result[0].l_err_code == "0") {
              let alert = this.alertCtrl.create({
                title: "提示",
                message: "删除成功",
                buttons: [
                  {
                    text: "确定",
                    role: 'cancel',
                    handler: () => {
                      this.storage.set("doRefresh", true);
                      this.navCtrl.pop();
                    }
                  }
                ]
              });
              alert.present();
            } else {
              let alert = this.alertCtrl.create({
                title: "提示",
                message: "删除失败，原因：" + result[0].vc_err_msg,
                buttons: [
                  {
                    text: "确定",
                    role: 'cancel',
                    handler: () => {
                      this.storage.set("doRefresh", true);
                      this.navCtrl.pop();
                    }
                  }
                ]
              });
              alert.present();
            }
          });
        }
      }]
    });
    confirm.present();
  }

  chooseListType() {
    let bottons = [{
      text: '编辑',
      handler: () => {
        this.navCtrl.push(MarketingLineupSavePage, {
          isEdit: true,
          detail: this.detail
        })
      }
    }, {
      text: '删除',
      role: 'destructive',
      handler: () => {
        this.delete();
      }
    }, {
      text: '取消',
      role: 'cancel',
      handler: () => {
      }
    }];

    let actionSheet = this.actionSheetCtrl.create({
      title: '',
      buttons: bottons
    });
    actionSheet.present();
  };


}
